package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.dao.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * 添加检查项
     * @param checkItem
     */
    public void addCheckItem(CheckItem checkItem) {
        checkItemMapper.addCheckItem(checkItem);
    }

    /**
     * 根据id删除检查项
     * @param id
     */
    public void deleteBooking(Integer id) {
        List<Integer> relatedCheckGroupIds = checkItemMapper.findRelatedCheckGroups(id);
        if (!relatedCheckGroupIds.isEmpty()) {
            for (Integer checkGroupId : relatedCheckGroupIds) {
                checkItemMapper.deleteCheckGroupCheckItem(checkGroupId, id);
            }
        }
        checkItemMapper.deleteBooking(id);
    }

    /**
     * 更新检查项
     * @param checkItem
     */
    public void updateBooking(CheckItem checkItem) {
        checkItemMapper.updateBooking(checkItem);
    }


    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> page = checkItemMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }



    @Override
    public List<CheckItem> getAllBooking() {
        return checkItemMapper.getAllBooking();
    }
    public CheckItem getBookingById(Integer id) {
        return checkItemMapper.getBookingById(id);
    }

    /**
     * 根据检查组id查询检查项id集合
     * @return
     */
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkGroupId) {
        return checkItemMapper.findCheckItemIdsByCheckGroupId(checkGroupId);
    }
}
